package com.tencent.qf.user.controller;

import com.alibaba.fastjson.JSON;
import com.tencent.qf.common.pojo.HttpResult;
import com.tencent.qf.user.dto.MidApiJsSdkInfo;
import com.tencent.qf.user.dto.MidApiUserInfo;
import com.tencent.qf.user.dto.WxpubSubscribeInfo;
import com.tencent.qf.user.dto.request.MidApiGetJsSdkRequest;
import com.tencent.qf.user.dto.request.MidApiGetUserByUnionidRequest;
import com.tencent.qf.user.dto.request.WxpubSubscribeInfoRequest;
import com.tencent.qf.user.service.MidApiUserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class MidApiUserController {


    @Autowired
    private MidApiUserService midApiUserService;

    @PostMapping("/api/user/wxpubSubscribeInfo")
    @ApiOperation("用户是否关注公众号")
    public HttpResult<WxpubSubscribeInfo> wxpubSubscribeInfo(@RequestBody WxpubSubscribeInfoRequest request) {
        log.info("用户是否关注公众号，入参：{}", JSON.toJSONString(request));
        MidApiGetUserByUnionidRequest midRequest = new MidApiGetUserByUnionidRequest();
        midRequest.setUnionid(request.getUnionId());
        midRequest.setWechatId(request.getAppId());
        MidApiUserInfo userInfo = midApiUserService.getByUnionid(midRequest);
        WxpubSubscribeInfo data = new WxpubSubscribeInfo();
        if(userInfo != null){
            if("1".equals(userInfo.getIf_sub())){
                data.setSubscribe("1");
            }else {
                data.setSubscribe("0");
            }
            data.setSubscribeTime(userInfo.getSub_time());
        }
        HttpResult<WxpubSubscribeInfo> result = HttpResult.success(data);
        log.info("用户是否关注公众号，出参：{}", JSON.toJSONString(result));
        return result;
    }

    @PostMapping("/api/user/js/sdk/get")
    @ApiOperation("获取jssdk")
    public HttpResult<MidApiJsSdkInfo> getJssdk(@RequestBody MidApiGetJsSdkRequest request) {
        log.info("获取jssdk，入参：{}", JSON.toJSONString(request));
        MidApiJsSdkInfo jssdk = midApiUserService.getJSSDK(request);
        return HttpResult.success(jssdk);
    }
}
